﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhanMemBus;
namespace Pham_mem_hoc_C
{
    public partial class Form_thuchanh : Form
    {
        private ThucHanhBus thBus = new ThucHanhBus();
        private LyThuyetBUS ltBus = new LyThuyetBUS();
       private DiemBus diemBus = new DiemBus();
        private KetQua kqua;
        int dong;
        string name;
     
        public Form_thuchanh()
        {
            InitializeComponent();
        }
        private void Form_thuchanh_Load(object sender, EventArgs e)
        {
            cay_thu_muc(treeTh);
            cay_thu_muc(tvDemuc);
            cay_thu_muc(tvViewCode);
            GV_Diem.DataSource = diemBus.DS_Diem();
        }
        //Tao cay thu muc
        public void cay_thu_muc(TreeView treeDM)
        {
            DataTable dtchuong= new DataTable();
            dtchuong=thBus.ten_chuong();
            int countC;
            countC = dtchuong.Rows.Count;
            for (int i = 0; i < countC; i++)
            {
                string tenChuong = dtchuong.Rows[i]["ten_kn"].ToString();
                DataTable dtBViet = thBus.ten_bai_viet(tenChuong);
                int countBV=dtBViet.Rows.Count;
                TreeNode treeC = new TreeNode(tenChuong);
                for (int j=0;j<countBV;j++)
                {
                    TreeNode treechild = new TreeNode();
                    treechild.Text = dtBViet.Rows[j]["ten_bhct"].ToString();
                    treeC.Nodes.Add(treechild);
                }
                treeDM.Nodes.Add(treeC);
            }
            treeDM.CollapseAll();
        }
        /*----------------------------------------------------------------------------------------------
         * Cac ham cua form thuc hanh
         * Author: Cao Thi Van Anh
         */
        
        //do noi dug vao richtextbox va dap an cho phan bai tap
        public void do_noi_dung(RichTextBox tb_noidung, CheckedListBox ck_dapan, Label dapan, DataRow dr)
        {
            tb_noidung.Text = dr["noi_dung"].ToString();
            dapan.Text = dr["dap_an"].ToString();
            ck_dapan.Items.Add(dr["phuong_an_A"].ToString());
            ck_dapan.Items.Add(dr["phuong_an_B"].ToString());
            ck_dapan.Items.Add(dr["phuong_an_C"].ToString());
            ck_dapan.Items.Add(dr["phuong_an_D"].ToString());
        }
        //lua chon bai tren cay thu muc
        private void treeTh_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string name = e.Node.Text;
            DataTable dt = thBus.noi_dung_bai_tap(name);
            int count = dt.Rows.Count;
            if (count > 0)
            {
                lb_tenbai.Text = name;
                do_noi_dung(rt_cau1, ck_cau1, lb_dapan1, dt.Rows[0]);
                do_noi_dung(rt_cau2, ck_cau2, lb_dapan2, dt.Rows[1]);
                do_noi_dung(rt_cau3, ck_cau3, lb_dapan3, dt.Rows[2]);
                //do_noi_dung(rt_cau4, ck_cau4, lb_dapan4, dt.Rows[3]);
                //do_noi_dung(rt_cau5, ck_cau5, lb_dapan5, dt.Rows[4]);
                //do_noi_dung(rt_cau6, ck_cau6, lb_dapan6, dt.Rows[5]);
            }
            else
                MessageBox.Show("Bạn chưa chọn bài tập");
        }
      
        //Ham kiem tra ket qua
        public int ketqua(CheckedListBox list, Label ketqua)
        {
            if (list.Text == ketqua.Text)
                return 10;
            else
                return 0;
        }
        //Khi bam nut kiem tra ket qua    
        private void bt_kiemtra_Click(object sender, EventArgs e)
        {
            int diem = 0;
            diem = ketqua(ck_cau1, lb_dapan1) + ketqua(ck_cau2, lb_dapan2) + ketqua(ck_cau3, lb_dapan3) + ketqua(ck_dapan4, lb_dapan4) + ketqua(ck_dapan5, lb_dapan5) + ketqua(ck_dapan6, lb_dapan6);
            kqua = new KetQua(lb_tenbai.Text, lb_dapan1.Text, lb_dapan2.Text, lb_dapan3.Text, lb_dapan4.Text, lb_dapan5.Text, lb_dapan6.Text, diem);
            if (kqua.ShowDialog() == DialogResult.OK)
            {
                GV_Diem.DataSource = diemBus.DS_Diem();
            }
        }
        //chuyen sang trang thuc hanh tiep theo
        private void btnTieptheo_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
        }
        // tim kiem trong tab thuc hanh
        private void bt_timkiemTH_Click_1(object sender, EventArgs e)
        {
            rt_NoidungTVTK.Text = "";
            GV_ketquaTKTH.DataSource = thBus.TimKiemTV(tb_Noidung_TKTH.Text);
        }
        // Xem chi tiet noi dung tim kiem
        private void GV_ketquaTKTH_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            int y = e.RowIndex;
            string noidung = GV_ketquaTKTH.Rows[y].Cells["ham"].Value.ToString();
            rt_NoidungTVTK.Text = noidung;
        }
        private void btncback_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = true;
        }
        //chuyen den trang ly thuyet
        private void bt_lythuyetTH_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage3;
        }
        //chuyen den trang vi du
        private void bt_viduTH_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage4;
        }
        //chuyen den trang chinh
        private void bt_trangchinhTH_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage2;
        } 
        //thoat khoi chuong trinh

        private void bt_thoatTh_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #region
        //Form Ly thuyet
        //Author Phung Xuan Thang
        //An va hien tim kiem
        private void bnAn_Click(object sender, EventArgs e)
        {
            gbTimKiem.Visible = false;
            bnHien.Enabled = true;
            bnAn.Enabled = false;
        }
        private void bnHien_Click(object sender, EventArgs e)
        {
            gbTimKiem.Visible = true;
            bnAn.Enabled = true;
            bnHien.Enabled = false;
        }
        private void LayCode(DataRow dr)
        {
           
                rtbndung.Text = dr["vi_du"].ToString();
                string tenbh = dr["duong_dan"].ToString();
                richTextBox1.Text = tenbh;
                string duongdan = Application.StartupPath + "\\Baihat\\" + tenbh;

                axWindowsMediaPlayer1.URL = duongdan;
                
        }
        // Do noi dung bai hoc
        public void DoNoiDung(DataRow dr)
        {

            rtbchucnangbh.Text = dr["chuc_nang"].ToString();
            rtbquytac.Text = dr["quy_tac"].ToString();
            rtbcuphap.Text = dr["cu_phap"].ToString();
            if (dr["pham_vi"].ToString().Length == 0)
            {
                label21.Visible = false;
                rtbPhamvi.Visible = false;
            }

            else
            {
                label21.Visible = true;
                rtbPhamvi.Visible = true;
                rtbPhamvi.Text = dr["pham_vi"].ToString();
            }

            if (dr["so_chu"].ToString().Length == 0)
            {
                label22.Visible = false;
                rtbsochu.Visible = false;
            }

            else
            {
                label22.Visible = true;
                rtbsochu.Visible = true;
                rtbsochu.Text = dr["so_chu"].ToString();
            }

            if (dr["kich_thuoc"].ToString().Length == 0)
            {
                label23.Visible = false;
                rtbkichthuoc.Visible = false;
            }

            else
            {
                label23.Visible = true;
                rtbkichthuoc.Visible = true;
                rtbkichthuoc.Text = dr["kich_thuoc"].ToString();
            }
               
         }
        // Do noi dung khai niem
        public void DoNoiDungkn(DataRow dr)
        {

            rtbDinhnghia.Text = dr["dinh_nghia"].ToString();
            rtbcn.Text = dr["chuc_nang"].ToString();


        }
        // Thoat khoi chuong trinh
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // Quay lai trang chinh
        private void btnBack_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage2;
        }
        // Ghi nho lai kien thuc
        private void btnTomTatLT_Click(object sender, EventArgs e)
        {
            Pham_mem_hoc_C.PhanMem.GhiNho frmghinho = new Pham_mem_hoc_C.PhanMem.GhiNho();
            frmghinho.Show();
        }
        // Huong dan tim kiem
        private void button2_Click(object sender, EventArgs e)
        {
            PhanMem.Huongdan hd = new PhanMem.Huongdan();
            hd.ShowDialog();
        }
        // Tim kiem
        private void button1_Click(object sender, EventArgs e)
        {
            if (rdbbaiviet.Checked == false)
            {
                dataGridView1.DataSource = ltBus.TimKiemTV(txtTimkiem.Text);
            }
            if (rdbbaiviet.Checked == true)
            {
                string str = cbotimkiem.Text.ToString();
                dataGridView1.DataSource = ltBus.TimKiemBV(str, txtTimkiem.Text);
            }
        }
        // Load de muc Form ly thuyet
        private void tvDemuc_AfterSelect(object sender, TreeViewEventArgs e)
        {

            name = e.Node.Text;
            DataTable dt = ltBus.NoiDungBH(name);
            int count = dt.Rows.Count;
            int num = dt.Columns.Count;
            if (count > 0)
            {
                gbNoiDung.Text = name;
                if (num > 3)
                {
                    pnbaihoc.Visible = true;
                    DoNoiDung(dt.Rows[0]);
                }
                  
                else
                {
                    pnbaihoc.Visible = false;
                    DoNoiDungkn(dt.Rows[0]);
                }
                  
             
            }
           

        }
        // Tim kiem theo bai viet
        private void rdbbaiviet_CheckedChanged(object sender, EventArgs e)
        {
            if(rdbbaiviet.Checked == true)
            {
                label14.Visible = true;
                cbotimkiem.Visible = true;
            }
        }
        // Tim kiem theo thu vien
        private void rdbThuvien_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbbaiviet.Checked == false)
            {
                label14.Visible = false;
                cbotimkiem.Visible = false;
            }
        }
        // Hien noi dung tim kiem
        private void btnHiennoidung_Click(object sender, EventArgs e)
        {
            try
            {
            gbNoiDung.Text = dataGridView1.Rows[dong].Cells["ten_bhct"].Value.ToString();
            rtbchucnangbh.Text = dataGridView1.Rows[dong].Cells["chuc_nang"].Value.ToString();
            rtbcuphap.Text = dataGridView1.Rows[dong].Cells["cu_phap"].Value.ToString();
            rtbquytac.Text = dataGridView1.Rows[dong].Cells["quy_tac"].Value.ToString();
           
           
          
            if (dataGridView1.Rows[dong].Cells["pham_vi"].Value.ToString().Length == 0)
            {
                label21.Visible = false;
                rtbPhamvi.Visible = false;
            }

            else
            {
                label21.Visible = true;
                rtbPhamvi.Visible = true;
                rtbPhamvi.Text = dataGridView1.Rows[dong].Cells["pham_vi"].Value.ToString();
            }

            if (dataGridView1.Rows[dong].Cells["kich_thuoc"].Value.ToString().Length == 0)
            {
                label22.Visible = false;
                rtbsochu.Visible = false;
            }

            else
            {
                label22.Visible = true;
                rtbsochu.Visible = true;
                rtbkichthuoc.Text = dataGridView1.Rows[dong].Cells["kich_thuoc"].Value.ToString();
            }

            if (dataGridView1.Rows[dong].Cells["so_chu"].Value.ToString().Length == 0)
            {
                label23.Visible = false;
                rtbkichthuoc.Visible = false;
            }

            else
            {
                label23.Visible = true;
                rtbkichthuoc.Visible = true;
                rtbsochu.Text = dataGridView1.Rows[dong].Cells["so_chu"].Value.ToString();
            }
            }
            catch 
            {
                MessageBox.Show("Chưa có nội dung");
            }
          
        }
        // Lay so dong cua bai viet trong tim kiem
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            try
            {
                int id = Convert.ToInt32(dataGridView1.Rows[dong].Cells["ma_bhct"].Value.ToString());

            }
            catch
            {
            }
            
            
        }
        // Load de muc Form vi du
        private void tvViewCode_AfterSelect(object sender, TreeViewEventArgs e)
        {
            name = e.Node.Text;
            DataTable dt = ltBus.NoiDungBH(name);
            int count = dt.Rows.Count;
            int num = dt.Columns.Count;
            if (count > 0)
            {
                groupBox6.Text = name;
                if(num > 3)
                     LayCode(dt.Rows[0]);
                else
                    rtbndung.Text = "";
            }
           
        }
        // Xu ly su kien khi kich vào xem code
        private void btnXemCode_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage4;
            DataTable dt = ltBus.NoiDungBH(name);
            int count = dt.Rows.Count;
            int num = dt.Columns.Count;
            if (count > 0)
            {
                groupBox6.Text = name;
                if (num > 3)
                {
                    LayCode(dt.Rows[0]);
                }
                else
                    rtbndung.Text = "";
                  
            }
        }
        // Xu ly su kien khi kich vao trang chinh
        private void button4_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage5;
        }
        // Chon xem ly thuyet trong vi du code
        private void button3_Click(object sender, EventArgs e)
        {

            tabControl1.SelectedTab = tabPage3;
            DataTable dt = ltBus.NoiDungBH(name);
            int count = dt.Rows.Count;
            int num = dt.Columns.Count;
            if (count > 0)
            {
                gbNoiDung.Text = name;
                if (num > 3)
                {
                    pnbaihoc.Visible = true;
                    DoNoiDung(dt.Rows[0]);
                }

                else
                {
                    pnbaihoc.Visible = false;
                    DoNoiDungkn(dt.Rows[0]);
                }


            }
        }
        // Lay ten khai niem va ten bai hoc hien thoi
        private void LaytenbhvaKn(DataRow dr)
        {
            string tenchuong = dr["ten_kn"].ToString();
            string tenbh = dr["ten_bhct"].ToString();
        }
        // Them code vao co so du lieu
        private void button5_Click(object sender, EventArgs e)
        {
            DataTable dt = ltBus.NoiDungBH(name);
            string tenchuong = dt.Rows[0]["ten_kn"].ToString();
            string tenbh = dt.Rows[0]["ten_bhct"].ToString();
            try
            {
                ltBus.Themvidu(tenchuong, tenbh, rtbndung.Text);
                MessageBox.Show("Them thanh cong!!!");
            }
            catch 
            {
                MessageBox.Show("Chang them duoc!!!");
              
            }
           
        }
        private void label25_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage3;
        }

        private void label27_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage5;
        }

        private void label29_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage4;
        }

        private void label26_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage1;
        }

        private void label28_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage6;
        }
        #endregion

       
        /*------------------------------------------------------------------------
               * Form điểm
               * Author:Cao Thị Vân Anh
               */
        
        public string dieu_kien()
        {
            string sql = "";
            string ketnoi = "";
            if (tb_ten_nguoihoc.Text != "")
            {
                sql += " ten_nguoi_dung like N'%" + tb_ten_nguoihoc.Text + "%'";
                ketnoi = " and ";
            }
            if (rd_ngaylam.Text != "")
                sql += ketnoi + " convert(nvarchar,ngay_lam,103) = '" + rd_ngaylam.Text + "'";
            return sql;
        }
     //   tim kiem danh sach diem
        private void bt_Tim_kiem_Click(object sender, EventArgs e)
        {
            GV_Diem.DataSource = diemBus.DS_Diem_TK(dieu_kien());
        }
        private void bt_refresh_Click(object sender, EventArgs e)
        {
            GV_Diem.DataSource = diemBus.DS_Diem();
        }
        private void bt_xoa_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in GV_Diem.Rows)
            {
                if (dr.Selected)
                {
                    int madiem = Convert.ToInt32(dr.Cells["MaDiem"].Value);
                    diemBus.Xoa_Diem(madiem);
                }
            }
            GV_Diem.DataSource = diemBus.DS_Diem();

        }
        private void tabControl1_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabPage6)
                GV_Diem.DataSource = diemBus.DS_Diem();
        }

        //private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        //{
        //    pnthanhvien.Visible = true;
        //}

        //private void linkLabel1_MouseDoubleClick(object sender, MouseEventArgs e)
        //{
        //    pnthanhvien.Visible = false;
        //}

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            pnthanhvien.Visible = true;
        }

        private void linkLabel1_DoubleClick(object sender, EventArgs e)
        {
            pnthanhvien.Visible = false;
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            pncaidat.Visible = true;
        }

        private void pictureBox7_DoubleClick(object sender, EventArgs e)
        {
            pncaidat.Visible = false;
        }



     }
}
    